package boot.controller;


import boot.domain.Product;
import boot.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author enthalpy
 * @since 2022-10-28
 */
@RestController
@RequestMapping("/products")
public class ProductController {
    final
    ProductServiceImpl productService;

    public ProductController(ProductServiceImpl productService) {
        this.productService = productService;
    }

    @GetMapping
    @PreAuthorize("hasAuthority('query')")
    public List<Product> getAll(){
        return productService.list();
    }

    @PostMapping
    @PreAuthorize("hasAuthority('update')")
    public boolean save(@RequestBody Product product){
        return productService.save(product);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('update')")
    public boolean update(@RequestBody Product product){
        return productService.update(product, null);
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('update')")
    public boolean delete(@PathVariable int id){
        return productService.removeById(id);
    }

    @GetMapping("{id}")
    public Product getById(@PathVariable int id){
        return productService.getById(id);
    }
}

